package org.evil.deus.rss.provider;

import org.evil.deus.rss.R;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class Provider extends ContentProvider {
	/*default*/ static final String AUTHORITY = "org.evil.deus.rss";
	
	private static final int MATCH_ITEMS = 0;
	private static final int MATCH_ITEMS_ID = 1;
	private static final int MATCH_CHANNELS = 2;
	private static final int MATCH_CHANNELS_ID = 3;
	private static final int MATCH_SEARCH = 4;
	
	private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
	static {
		MATCHER.addURI(AUTHORITY, RSS.Item.TABLE, MATCH_ITEMS);
		MATCHER.addURI(AUTHORITY, RSS.Item.TABLE + "/*", MATCH_ITEMS_ID);
		MATCHER.addURI(AUTHORITY, RSS.Channel.TABLE, MATCH_CHANNELS);
		MATCHER.addURI(AUTHORITY, RSS.Channel.TABLE + "/*", MATCH_CHANNELS_ID);
		MATCHER.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", MATCH_SEARCH);
	}
	
	private static final String [] SEARCH_PROJECTION = new String [] {
		RSS.Item._ID,
		RSS.Item.TITLE + " as " + SearchManager.SUGGEST_COLUMN_TEXT_1,
		RSS.Item.CATEGORY + " as " + SearchManager.SUGGEST_COLUMN_TEXT_2,
		R.drawable.icon + " as " + SearchManager.SUGGEST_COLUMN_ICON_1,
		RSS.Item.LINK + " as " + SearchManager.SUGGEST_COLUMN_INTENT_DATA
	};
	
	private static final String SEARCH_SELECTION = 	SearchManager.SUGGEST_COLUMN_TEXT_1 + " LIKE ? OR " + 
													SearchManager.SUGGEST_COLUMN_TEXT_2 + " LIKE ?";
	
	private ProviderHelper helper = null;
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String table = RSS.Item.TABLE;
		switch (MATCHER.match(uri)) {
			case MATCH_ITEMS:
				break;
			case MATCH_ITEMS_ID:
				String _id = uri.getLastPathSegment();
				selection = RSS.Item._ID + " = ?";
				selectionArgs = new String [] {_id};
				break;
			case MATCH_CHANNELS:
				table = RSS.Channel.TABLE;
				break;
			case MATCH_CHANNELS_ID:
				table = RSS.Channel.TABLE;
				_id = uri.getLastPathSegment();
				selection = RSS.Item._ID + " = ?";
				selectionArgs = new String [] {_id};
				break;
			default:
				throw new UnsupportedOperationException();
		}
		int result = db.delete(table, selection, selectionArgs);
		getContext().getContentResolver().notifyChange(uri, null);
		return result;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String table = RSS.Item.TABLE;
		String nullColumn = RSS.Item._ID;
		Uri baseUri = RSS.Item.CONTENT_URI;
		switch (MATCHER.match(uri)) {
			case MATCH_ITEMS:
				break;
			case MATCH_CHANNELS:
				table = RSS.Channel.TABLE;
				nullColumn = RSS.Channel._ID;
				baseUri = RSS.Channel.CONTENT_URI;
				break;
			default:
				throw new UnsupportedOperationException();
		}
		long id = db.insert(table, nullColumn, values);
		Uri result = id != -1 ? Uri.withAppendedPath(baseUri, String.valueOf(id)) : null;
		getContext().getContentResolver().notifyChange(uri, null);
		return result;
	}

	@Override
	public boolean onCreate() {
		helper = new ProviderHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = helper.getReadableDatabase();
		String table = RSS.Item.TABLE;
		switch (MATCHER.match(uri)) {
			case MATCH_ITEMS:
				sortOrder = sortOrder != null ? sortOrder : RSS.Item.DEFAULT_ORDER;
				break;
			case MATCH_ITEMS_ID:
				String _id = uri.getLastPathSegment();
				selection = RSS.Item._ID + " = ?";
				selectionArgs = new String [] {_id};
				sortOrder = sortOrder != null ? sortOrder : RSS.Item.DEFAULT_ORDER;
				break;
			case MATCH_CHANNELS:
				table = RSS.Channel.TABLE;
				sortOrder = sortOrder != null ? sortOrder : RSS.Channel.DEFAULT_ORDER;
				break;
			case MATCH_CHANNELS_ID:
				table = RSS.Channel.TABLE;
				_id = uri.getLastPathSegment();
				selection = RSS.Channel._ID + " = ?";
				selectionArgs = new String [] {_id};
				sortOrder = sortOrder != null ? sortOrder : RSS.Channel.DEFAULT_ORDER;
				break;
			case MATCH_SEARCH:
				String query = uri.getLastPathSegment();
				projection = SEARCH_PROJECTION;
				selection = SEARCH_SELECTION;
				selectionArgs = new String [] {"%"+query+"%", "%"+query+"%"};
				sortOrder = SearchManager.SUGGEST_COLUMN_TEXT_1 + " asc";
				break;
			default:
				throw new UnsupportedOperationException();
		}
		Cursor result = db.query(table, 
								 projection, 
								 selection, 
								 selectionArgs, 
								 null, 
								 null, 
								 sortOrder);
		result.setNotificationUri(getContext().getContentResolver(), uri);
		return result;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String table = RSS.Item.TABLE;
		switch (MATCHER.match(uri)) {
			case MATCH_ITEMS:
				break;
			case MATCH_ITEMS_ID:
				String _id = uri.getLastPathSegment();
				selection = RSS.Item._ID + " = ?";
				selectionArgs = new String [] {_id};
				break;
			case MATCH_CHANNELS:
				table = RSS.Channel.TABLE;
				break;
			case MATCH_CHANNELS_ID:
				table = RSS.Channel.TABLE;
				_id = uri.getLastPathSegment();
				selection = RSS.Item._ID + " = ?";
				selectionArgs = new String [] {_id};
				break;
			default:
				throw new UnsupportedOperationException();
		}
		int result = db.update(table, values, selection, selectionArgs);
		getContext().getContentResolver().notifyChange(uri, null);
		return result;
	}
}
